<?php 
# $Id$
require_once(APP_LIB_DIR . 'Sajax.php');
require_once('utils.inc.php');

function getlist($p='',$search='',$by='1',$s='DESC') { 
    $a = new TimeSettingsAction();
    parse_str($search,$res);
    return $a->getlist($p,$res,$by,$s); 
}
function lookup($what=''){
    $l = new TimeSettingsAction();
    return $l->lookup($what);
}
function save($detail='',$id='',$action=''){
    $s = new TimeSettingsAction();
    parse_str($detail,$res);
    return $s->save($res,$id,$action);
}

sajax_init();
//$sajax_debug_mode = 1;
sajax_export('getlist');
sajax_export('lookup');
sajax_export('save');
sajax_handle_client_request();
$sajax_request_type = 'POST';
require_once('header.php'); 
?>
<script language="JavaScript" type="text/javascript">

<?php sajax_show_javascript();?>

var s = "ASC";
var b = 1;

function page(p) {
    st = $('form1').toQueryString();
    load();
    x_getlist(p,st,b,s,getlist_cb)
}
function sort(by){
    s = (s=="DESC") ? "ASC" : "DESC";
    st = $('form1').toQueryString();
    p = $('pagenum');
    p = (p) ? p.value : 1;
    b = by;
    load();
    x_getlist(p,st,b,s,getlist_cb)
}
function filter(){
    st = $('form1').toQueryString();
    p = 1
    //st = Json.toString(st);
    load();
    x_getlist(p,st,b,s,getlist_cb);
}
function getlist_cb (new_data){
    //alert(new_data)
    data = Json_eval(new_data);
    var fields = [ ["id","ID"],
                   ["name","Transaction"],
                   ["time_from","Time From"],
                   ["time_to","Time To"],
                   ["preset_message","Preset Message"]

                  // ["default","Status"]
		  	
                 ]
    var  extra = [ [true,'id','edit'],
                   [false,'id','del']
                 ]
                 
    $("thetable").innerHTML = data[1]
    $("thetable").innerHTML += mytable(data[0],fields, extra)
}

/*function levels_cb (new_data){
    data = Json_eval(new_data);
    AddToOptionList(data, $('level'), 'id','name')
}*/

function save () {
data = $('form2').toQueryString();
id = $('myid').value
$('myid').value = ''

//d = $('default').value

/*if(d=='0') {*/
    x_save(data,id, function(new_data1){
      Json_eval(new_data1);
      filter();
      show_t()
    });
/*}else{
    x_save('','','check_prevdefault', function(new_data1){
       d = Json_eval(new_data1);
alert (d)
       if(d){
         x_save('',id,'update_prevdefault', function(new_data1){
           d = Json_eval(new_data1);
             x_save(data,id, function(new_data1){
               Json_eval(new_data1);
               filter();
               show_t()
             });
         });
       }
    });

})*/

}
function show_t(){
    if($('thetable').style.display == "none"){
        hide_t('newform','thetable')
    //    $('altfrm').value = "New Contacts"
    } else {
        hide_t('thetable','newform')
     //   $('altfrm').value = "Back to List"
    }
    $('form2').reset();
}

function edit (id) {
    x_save('',id,'get', function(new_data1){
       d = Json_eval(new_data1);
       $('name').value = d[0]['name'];
       $('time_from').value = d[0]['time_from'];
       $('time_to').value = d[0]['time_to'];
       $('presetmsg').value = d[0]['presetmsg_id'];
       $('preset_message').value = d[0]['preset_message'];
      // $('default').value = d[0]['default'];
       $('myid').value = d[0]['id'];
      // optionSelected($('default'), d[0]['default'])
       hide_t('thetable','newform');
    }) 
    return false;
}

function del (id) {
    if (confirm('Are you sure you want to delete this Record?')){
      x_save('',id,'del', function(new_data1){
        Json_eval(new_data1);
        filter();
      })   
    }
}

function load (){
    //$("thetable").empty().addClass('ajax-loading');
    $("thetable").innerHTML = loading;
}

</script>
<link rel="stylesheet" href="<?=WEB_MODULE_DIR?>css/structure2.css" type="text/css" />
<link rel="stylesheet" href="<?=WEB_MODULE_DIR?>css/form2.css" type="text/css" />
<!--<link rel="stylesheet" href="<?=WEB_MODULE_DIR?>css/theme2.css" type="text/css" />-->
<table width="100%" class="menubar" cellpadding="0" cellspacing="0" border="0">
    <tr>
        <td class="menudottedline" width="40%">
            <div class="pathway"><font size="2px" color="#000033"><b><?=$config['version']?></b></font></div>       
        </td>
        <td class="menudottedline" align="right">
            <table cellpadding="0" cellspacing="0" border="0" id="toolbar">
                <tr height="60" valign="middle" align="center">
                    <td>
                        <a class="toolbar" href="#">
                        <img src="<?=WEB_MODULE_DIR?>images/help_f2.png" 
                             align="middle" name="new" border="0"><br>Help
                        </a>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>
<br>
<br>
<div align="center" class="centermain">
    <div class="main">
        <table class="adminheading">
            <tr>
                <th class="cpanel" rowspan="2" nowrap>Time Settings&nbsp;<font size="1px" color="#CCCCCC">[<?=strtoupper($auth['privilege'])?>]</font></th>
            </tr>
        </table>
    </div>
        <table class="adminlist">
          <tr>
            <th class="title"><a href="javascript: show_tool('panel');">:: SEARCH PANEL</a></th>
          </tr>
          <tr>
            <td width="40%" valign="top">
            <!-- filter panel -->
		  <div id="panel">
		    <table class="adminlist">
		      <tr>
		        <td>
			  <form id="form1" class="wufo" action="" method="post" onSubmit="filter();return false;">
			    <ul>
			      <li>
			        <span>
			          <select class="field select" style="width:10em" name="search[searchfield]">
		                    <option value="name">Nama</option>
			          </select>
                                </span>

                                <span>
                                  <input class="field text" size="15" name="search[searchtext]" value="" onKeyup="filter()" />
                                </span>

                                <span>
                                  <input id="saveForm" class="btTxt" type="button" value="Search" onClick="filter()"/>
                                  <input class="btTxt" type="button" value="Clear" onClick="this.form.reset();filter()"/>
                                 <!-- <input id="altfrm" class="btTxt" type="button" value="New Contact" onClick="show_t()"/>-->
                                </span>
                              </li>
                            </ul>
                          </form>
			</td>
		      </tr>
		    </table>
		  </div>
			<!-- filter panel end -->
		</td>
	    </tr>
	</table>
        <table class="adminform">
            <tr>
                <th class="title">:: SEARCH RESULTS</th>
            </tr>
            <tr>
              <td>
                      <div id="thetable">
                        <img src="/mcs/modpub/images/loading_small.gif"/>
                      </div>
              </td>
	    </tr>
        </table>
        <table> 
            <tr>
              <td align="center">
                <div id="newform" style="display: none" class="container">
                  <h1>New</h1>
                        <form id="form2" class="wufoo" method="post" onSubmit="return false;">
                          <input type="hidden" value="" id="myid" />
                            <div class="info">
                              <h2>Time Settings</h2>
                                <!--<p>Create new Template</p>-->
                            </div>
                              <ul>
                                <li>
                                  <label class="desc">Name</label>
                                    <div>
                                     <!-- <textarea name ="details[name]" id="name" style="height: 90px; width: 300px;"> </textarea>-->
                                      <input class="field text large" type="text" id="name" name="details[name]" maxlength="255" value=""/ readonly>
                                    </div>
                                </li>
                                <li>
                                  <div>
                                    <span>
                                      <input class="field text large" type="text" id="time_from" name="details[time_from]" maxlength="255" value=""/>
                                    </span>
                                  </div> 
                                </li>
                                <li>
                                  <div>
                                    <span>
                                      <input class="field text large" type="text" id="time_to" name="details[time_to]" maxlength="255" value=""/>
                                    </span>
                                  </div> 
                                </li>
                                <li>
                                  <div>
                                    <span>
                                      <label class="desc">Preset Message</label>
                                        <div>
                                           <textarea name ="preset_message" id="preset_message" style="height: 90px; width: 300px;"> </textarea>
                                          <input type="hidden" id="presetmsg" name="details[presetmsg_id]" value="0"/>
                                          <input type="button" name="revise" class="btnformat" value="..."
                                          onClick="popUp('?module=<?=DEFAULT_MODULE?>&action=PresetMessagesLookup'); return false;">
                                        </div>
                                    </span>
                                  </div>
                                </li>
                                <li>
                                  <span>
                                    <input id="saveForm" class="btTxt" type="button" value="Save" onClick="save();"/>
                                    <input id="saveForm" class="btTxt" type="reset" value="Clear" onClick=""/>
                                    <input id="saveForm" class="btTxt" type="button" value="Cancel" onClick="hide_t('newform', 'thetable')"/>
                                  </span>
                                </li>
                              </ul>
                        </form>
                    </div>
              </td>
            </tr>
        </table>
<script type="text/javascript">
x_getlist(getlist_cb)
//x_lookup(levels_cb)
</script>

<?php require_once('footer.php'); ?>
